#include "calServer.hpp"
#include "data.hpp"
#include <memory>
using namespace server;

static void Usage(char *proc)
{
    cout << "/n/t" << proc << " local port error" << endl;
}
bool calfunc(const Request &resq, Response &resp)
{
    resp._exitcode = PERICOUS;
    switch (resq._op)
    {
    case '+':
    {
        resp._exitrest = resq._x + resq._y;
        break;
    }
    case '-':
    {
        resp._exitrest = resq._x - resq._y;
        break;
    }
    case '*':
    {
        resp._exitrest = resq._x * resq._y;
        break;
    }
    case '/':
    {
        if (resq._y == 0)
            resp._exitcode = DIV_ZERO;
        else
            resp._exitrest = resq._x / resq._y;
        break;
    }
    case '%':
    {
        if (resq._y == 0)
            resp._exitcode = MOD_ZERO;
        else
            resp._exitrest = resq._x % resq._y;
        break;
    }
    default:
        resp._exitcode = INPUT_ERROR;
        break;
    }
    return true;
}

// 输入的形式为 ./tcpServer 8080的格式
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(-1);
    }
    unique_ptr<calserver> tser(new calserver(atoi(argv[1]))); // 智能指针
    tser->initServer();                                       // 初始化操作
    tser->runServer(calfunc);                                      // 启动服务端

    return 0;
}